from rest_framework import serializers
from question.serialzers import Label, Reply, ReplySerializers, UserSimpleSerializers, QuestionSerializers
from recruit.serializers import EnterpriseSimpleSerializers
from article.serializers import UserArticleSerializers, User, Article


class LabelS(serializers.ModelSerializer):
    class Meta:
        model = Label
        fields = ['id', 'label_name']


class ReplyS(serializers.ModelSerializer):
    user = UserSimpleSerializers(read_only=True)
    subs = ReplySerializers
    class Meta:
        model = Reply
        fields = '__all__'


class ArticleS(serializers.ModelSerializer):
    user = UserArticleSerializers(read_only=True)
    class Meta:
        model = Article
        fields = '__all__'


# 个人详情
class UserDetailSerializer(serializers.ModelSerializer):
    labels = LabelS(read_only=True, many=True)
    questions = QuestionSerializers(read_only=True, many=True)
    answer_question = serializers.SerializerMethodField()
    collected_articles = ArticleS(read_only=True, many=True)
    enterpises = EnterpriseSimpleSerializers(many=True, read_only=True)

    class Meta:
        model = User
        fields = '__all__'

    def get_answer_question(self, obj):
        reply = Reply.objects.filter(user=obj, type__in=[0,1,2])
        return ReplyS(reply, many=True).data